#!/bin/sh
#package:odpkg
#desc:Update installed packages
#type:multiple

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later


PKGSTORAGE="/mnt/odconf/pkgcache"
URLLIST="/tmp/urlavailable.txt"
PIDFILE="/var/run/updatepackages.pid"

if ! test -e /var/pkg/installed
then
	echo "#ERR: [/var/pkg/installed] does not exist "
	exit 1
fi

if test -f $PIDFILE; then
	echo "#ERR: Operation in process"
#	echo "actions:" #to show the toolbar
	echo
	exit 2
else
	echo "#LOADING Operation in progress. Please wait"
#	echo "actions:" #to show the toolbar
	echo 
	echo "1" > $PIDFILE
fi

if test "$1" = "force"
then
	rm $URLLIST
fi

if ! test -f $URLLIST
then
	# La actualización de listas de paquetes es obligatoria
	. /usr/local/opendomo/updatePackageList.sh 
fi

if ! test -d "$PKGSTORAGE"
then
	echo "#WARN: [$PKGSTORAGE] does not exist"
	echo "#WARN: Installed packages will be lost after restart!"
	PKGSTORAGE="/tmp/"
fi

if ! touch "$PKGSTORAGE/test.log" 2>/dev/null
then
	echo "#ERR: [$PKGSTORAGE] is read-only"
	PKGSTORAGE="/tmp/"
fi
rm "$PKGSTORAGE/test.log"

#if test -d /etc/opendomo/langfiles/; then
#	rm /etc/opendomo/langfiles/*
#fi

updatepkg(){
	#echo "#$i version ($INSTALLEDVER) is installed"
	CANDIDATE=`grep "$PKGNAME-" $URLLIST | sort -r | cut -f 2 -d";" | head -n 1`
	CNUM=`grep "/$PKGNAME" $URLLIST | wc -l`
	#echo "# $CNUM packages found in $URLLIST"
	if test -z "$CANDIDATE"
	then
		return 1
	fi

	for c in $CANDIDATE
	do
		CANDIDATEVER=`basename "$c" |cut -f 2 -d'-'| cut -f 1 -d'.'`
		SHA1=`grep $c $URLLIST | cut -f 3 -d';' | head -n 1`
		#echo "#CANDIDATEVER: $i ($CANDIDATEVER - $CANDIDATESHA1) versus $INSTALLEDVER "
		if test "$CANDIDATEVER" -gt "$INSTALLEDVER"; then
			#echo "#Candidate version of $i is higher than installed one"
			BASENAME=`basename $c`
			CODE=`echo $BASENAME | cut -f1 -d'-'`
			LFILE="$PKGSTORAGE/$BASENAME"
			# Si ya tenemos un archivo con ese nombre, es antiguo
			rm $PKGSTORAGE/$CODE-* 2>/dev/null 
			rm -f $LFILE
		  	if wget -q ${c} -O $LFILE 2>/dev/null; then
	  			#echo "# Installing [$CODE] ..."
	  			if test -z "$SHA1"; then
	  				wget -q ${c}.sha1 -O /tmp/$BASENAME.sha1 2>/dev/null
					SHA1=`cat /tmp/$BASENAME.sha1`
					rm /tmp/$BASENAME.sha1
	  			fi
		  		SUM1=`sha1sum $LFILE | cut -f1 -d" "`
		  		if test "$SUM1" != "$SHA1"; then
#		  			echo "#ERR: Sums does not match:"
#		  			echo "#ERR: Calculated [$SUM1]"
#		  			echo "#ERR: Downloaded [$SHA1]"
		  			echo "	$1	$1	error	Sums differ ($SUM1 != $SHA1)"
		  			continue
		  		fi
		  		if pkg_add $LFILE >/dev/null 2>/dev/null; then
		  			#echo "	$1	$1	notice	Package updated"
					/usr/local/opendomo/discovery/processServices.sh
		  			echo "#INF: Package [$CODE] updated"
		  			return 0
		  		else
		  			#echo "	$1	$1	warning	Needs restart"
		  			echo "#INF: Needs restart"
		  			continue
		  		fi
		  		#Package has been automatically updated
		  	else
	  			#echo "	$1	$1	error	Cannot download [$CANDIDATE]"
		  		echo "#ERR: Error downloading package [$CANDIDATE]"
		  		#echo "# [$BASENAME $CANDIDATE]"
		  		continue
		  	fi
		#else
  		#	echo "	$1	$1	notice	Up to date"
		#	echo "#Package $i is up to date"
		fi
	done
}
echo 
echo "#> Updating installed packages"
echo "list:`basename $0`	detailed"
for i in /var/pkg/installed/* 
do 
	PKGNAME=`basename $i`
	if test -f /var/pkg/installed/$PKGNAME
	then
		INSTALLEDVER=`cat /var/pkg/installed/$PKGNAME | sed 's/_//g' | cut -f 1 -d.`
		updatepkg $PKGNAME $INSTALLEDVER
		sync
	fi
done	
echo "# Packages updated"
echo


#if test -x /usr/local/opendomo/services/syscript/updateLanguageFiles.sh
#then
#	echo "#INF: Updating language files"
#	/bin/logevent notice odpkg "Updating language files"
#	if test `whoami` = "root"; then
#		su -c /usr/local/opendomo/services/syscript/updateLanguageFiles.sh admin
#	else
#		/usr/local/opendomo/services/syscript/updateLanguageFiles.sh
#	fi
#fi

#if test -x /usr/local/opendomo/discovery/processServices.sh
#then
#	/bin/logevent notice odpkg "Processing services"
#	if test `whoami` = "root"; then
#		/bin/logevent notice odpkg "Processing services -root-"
#		su -c /usr/local/opendomo/discovery/processServices.sh admin >/dev/null
#	else
#		/bin/logevent notice odpkg "Processing services -$USER-"
#		/usr/local/opendomo/discovery/processServices.sh >/dev/null 2>/dev/null
#	fi
#fi

# Limpiamos permisos en el directorio /tmp/
chown admin /tmp/*.info 2>/dev/null
chown admin /tmp/*.txt 2>/dev/null
chgrp admin /tmp/*.info 2>/dev/null
chgrp admin /tmp/*.txt 2>/dev/null
chown admin /tmp/*.gz 2>/dev/null
chown admin /tmp/*.gz.sha1 2>/dev/null
rm -fr /tmp/tmppkglist 2>/dev/null


/bin/logevent notice odpkg "Triggering system calls"
/usr/bin/udevadm control --reload 2>/dev/null
/usr/bin/udevadm trigger 2>/dev/null
/sbin/ldconfig 2>/dev/null

chown admin $PKGSTORAGE/* 2>/dev/null
echo "actions:" #to show the toolbar
echo 
rm $PIDFILE
